<?php
/**
 * 文件名：photo.mod.php
 * 版本号：1.0
 * 作者：mrasong
 * 功能描述：视频模块
 */
if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject{
	var $ID = 0;

	function ModuleObject($config)
	{
		$this->MasterObject($config);
		if(isset($this->Get['id'])){
			$this->ID = (int)$this->Get['id'];
		}elseif(isset($this->Post['id'])){
			$this->ID = (int)$this->Post['id'];
		}
		
		Load::logic('topic');
		$this->TopicLogic = new TopicLogic();
		
				$this->my = array();
		if (MEMBER_ID < 1) {
			$this->Messager("请先登录");	
		}
		
		$this->my = $this->TopicLogic->GetMember(MEMBER_ID);
		$this->album_cate = array("产品","娱乐","风景","人像");
		
		$this->Execute();
	}

	function Execute()
	{
		ob_start();
		
		switch($this->Code)
		{
			case "my":
				$this->my();
				break;
			case "my_new":
				$this->my_new();
				break;
			case "my_movement":
				$this->my_movement();
				break;				
			case "square":
				$this->square();
				break;
			case "album":
				$this->album();
				break;
			case "set_cover":
				$this->set_cover();
				break;
			case "album_edit":
				$this->album_edit();
				break;
			case "album_del":
				$this->album_del();
				break;
			case "pic_del":
				$this->pic_del();
				break;
			case "upload":
				$this->upload();
				break;
			case "photo_save":
				$this->photo_save();
				break;
			case "comm_add":
				$this->comm_add();
				break;
			case "comm_del":
				$this->comm_del();
				break;
			default:
				$this->Main();
				break;
		}
		$Contents=ob_get_clean();
		$this->ShowBody($Contents);
	}

	function Main(){
	
		$sql = "SELECT a.*, b.title, c.nickname,c.username,c.face from `".TABLE_PREFIX."photo` as a 
				left join `".TABLE_PREFIX."photo_album` as b on a.albumId = b.id
				left join `".TABLE_PREFIX."members` as c on a.userId = c.uid
				where userId={$_COOKIE['MEMBER_ID']} order by a.id desc limit 20";		

		$query = $this->DatabaseHandler->Query($sql);
		$info_new = array();
		while ($row = $query->GetRow()){
				
			$sqlaa = "select count(*) `count_comm` from `".TABLE_PREFIX."photo_comment` where photoId in
					 (select id from `".TABLE_PREFIX."photo` where userId ={$row['userId']}) order by id desc";

			$queryA = $this->DatabaseHandler->Query($sqlaa);
			extract($queryA->GetRow());
			$row['comm_count'] = $count_comm;
			
			$info_new[$row['nickname']][] = $row;
		}
		
		include(ROOT_PATH . './templates/app/photo_index.php');
	}
	
	function upload(){ //上传图片
		$sql = "SELECT * from `".TABLE_PREFIX."photo_album` where uid={$_COOKIE['MEMBER_ID']}";
		$query = $this->DatabaseHandler->Query($sql);
		
		$photo_album = array();
		while ($row = $query->GetRow()){
			$photo_album[] = $row;
		}
		
		include(ROOT_PATH . './templates/app/photo_upload.php');
	}

	
	function my(){
		$where = "";
		$query_link = "index.php?mod=photo&code=my";

		$page_size = 16;
		$page = empty($this->Get['page']) ? 1 : $this->Get['page'];

		$sql = "select count(*) as `total_album` from `".TABLE_PREFIX."photo_album` where uid={$_COOKIE['MEMBER_ID']}";
		$query = $this->DatabaseHandler->Query($sql);
        extract($query->GetRow());
		$total_page = ceil($total_album/$page_size);
		$page = $page>$total_page ? $total_page : $page;
		
		$page_arr = pagebrand($total_album,$page_size,$query_link,array('return'=>'array',));
		
		$sqla = "select * from `".TABLE_PREFIX."photo_album` where uid={$_COOKIE['MEMBER_ID']} order by addtime desc {$page_arr['limit']}";
		$querya = $this->DatabaseHandler->Query($sqla);

	    $album=array(); 
		while($rowa = $querya->GetRow())
		{	
			$sql_first = "select id,picUrl from `".TABLE_PREFIX."photo` where albumId={$rowa['id']} order by id desc limit 1";
			$query_first = $this->DatabaseHandler->Query($sql_first);
			$pInfoFirst = $query_first->GetRow(); //图片信息
			$pInfoFirst['picUrl'] = empty($pInfoFirst['picUrl']) ? "images/nopic.gif" : "upload/photo/{$pInfoFirst['picUrl']}";
			$rowa['pic'] = empty($rowa['pic']) ? $pInfoFirst['picUrl'] : "upload/photo/{$rowa['pic']}";
			$rowa['album_first'] = empty($pInfoFirst['id']) ? "#" : "index.php?mod=photo&code=album&id={$pInfoFirst['id']}";
			
			$sqlaa = "select count(*) `count_comm` from `".TABLE_PREFIX."photo_comment` where photoId in
					 (select id from `".TABLE_PREFIX."photo` where albumId ={$rowa['id']})";
			$queryA = $this->DatabaseHandler->Query($sqlaa);
			extract($queryA->GetRow());
			$rowa['comm_count'] = $count_comm;
			
			$sqlbb = "select count(*) `count_pics` from `".TABLE_PREFIX."photo` where albumId = {$rowa['id']}";
			$queryB = $this->DatabaseHandler->Query($sqlbb);
			extract($queryB->GetRow());
			$rowa['pics_count'] = $count_pics;
			
			
			$album[]=$rowa;
		}
		
		/* 编辑用 */
		if( !$id = empty($this->Get['id']) ){
			$sqla = "SELECT * from `".TABLE_PREFIX."photo_album` where id=$id";
			$query = $this->DatabaseHandler->Query($sql);
			$albumInfo = $query->GetRow();
		}
		
		include(ROOT_PATH . './templates/app/photo_my.php');
	}
	
	function my_new(){
		$sqlaa = "select count(*) as `total_album` from `".TABLE_PREFIX."photo_album` where uid={$_COOKIE['MEMBER_ID']}";
		$queryaa = $this->DatabaseHandler->Query($sqlaa);
        extract($queryaa->GetRow());
		
		$sqlpp = "select count(*) as `total_photo` from `".TABLE_PREFIX."photo` where userId={$_COOKIE['MEMBER_ID']}";
		$querypp = $this->DatabaseHandler->Query($sqlpp);
        extract($querypp->GetRow());
		
		
		$sql = "SELECT * from `".TABLE_PREFIX."photo` where userId={$_COOKIE['MEMBER_ID']} order by addTime desc limit 20";
		$query = $this->DatabaseHandler->Query($sql);
		
		$photo_new = array();
		while ($row = $query->GetRow()){
			$photo_new[] = $row;
		}
	
		include(ROOT_PATH . './templates/app/photo_my_new.php');
	}
	
	function my_movement(){
		$sql = "SELECT a.*, b.title from `".TABLE_PREFIX."photo` as a 
				left join `".TABLE_PREFIX."photo_album` as b on a.albumId = b.id
				where userId={$_COOKIE['MEMBER_ID']} order by a.id desc limit 20";
		$query = $this->DatabaseHandler->Query($sql);
		
		$message_new = array();
		while ($row = $query->GetRow()){
				
			$sqlaa = "select count(*) `count_comm` from `".TABLE_PREFIX."photo_comment` where photoId in
					 (select id from `".TABLE_PREFIX."photo` where albumId ={$row['albumId']}) order by id desc";
			$queryA = $this->DatabaseHandler->Query($sqlaa);
			extract($queryA->GetRow());
			$row['comm_count'] = $count_comm;
			
			$message_new[$row['title']][] = $row;
		}
			
		include(ROOT_PATH . './templates/app/photo_my_movement.php');
	}
	
	function square(){
		$album_cate = $this->album_cate;
			$where = "";
			$query_link = "index.php?mod=photo&code=square";
		if( in_array( base64_decode($this->Get['id']), $album_cate ) ){
			$query_link = "index.php?mod=photo&code=square&id={$this->Get['id']}";
			$where = "where category='".base64_decode($this->Get['id'])."'";
		}
	
		$page_size = 16;
		$page = empty($this->Get['page']) ? 1 : $this->Get['page'];

		$sql = "select count(*) as `total_album` from `".TABLE_PREFIX."photo_album` $where";
		$query = $this->DatabaseHandler->Query($sql);
        extract($query->GetRow());
		$total_page = ceil($total_album/$page_size);
		$page = $page>$total_page ? $total_page : $page;
		
		$page_arr = pagebrand($total_album,$page_size,$query_link,array('return'=>'array',));
		
		$sqla = "select a.*,b.nickname from `".TABLE_PREFIX."photo_album` as a 
			left join `".TABLE_PREFIX."members` as b on a.uid = b.uid $where order by addtime desc {$page_arr['limit']}";
		$querya = $this->DatabaseHandler->Query($sqla);

	    $album_list=array(); 
		while($rowa = $querya->GetRow())
		{	
		
			$sql_first = "select id,picUrl from `".TABLE_PREFIX."photo` where albumId={$rowa['id']} order by id desc limit 1";
			$query_first = $this->DatabaseHandler->Query($sql_first);
			$pInfoFirst = $query_first->GetRow(); //图片信息
			$pInfoFirst['picUrl'] = empty($pInfoFirst['picUrl']) ? "images/nopic.gif" : "upload/photo/{$pInfoFirst['picUrl']}";
			$rowa['pic'] = empty($rowa['pic']) ? $pInfoFirst['picUrl'] : "upload/photo/{$rowa['pic']}";
			$rowa['album_first'] = empty($pInfoFirst['id']) ? "#" : "index.php?mod=photo&code=album&id={$pInfoFirst['id']}";

			$album_list[]=$rowa;
		}			
		include(ROOT_PATH . './templates/app/photo_square.php');
	}
	

	function photo_save(){
		$photo_data = $this->Post['photo_data'];
		$photo_album = $this->Post['photo_album'];
		if( !empty($photo_data) ){
			$arr = explode("|",$photo_data);
			foreach( $arr as $photo ){
				$sql = "insert into `".TABLE_PREFIX."photo` (`userId`, `albumId`, `picUrl`, `addtime`) 
				values ( {$_COOKIE['MEMBER_ID']}, $photo_album, '$photo', '". date('Y-m-d H:i:s') ."' )";
				$query = $this->DatabaseHandler->Query($sql);
			}
			$html = "<script type='text/javascript'>alert('图片上传成功，本次共上传".count($arr)."张！');location.href='index.php?mod=photo&code=my';</script>";
			echo $html;
		}	
	}
	
	function set_cover(){
		$id = $this->Get['id'];
		$aid = $this->Get['aid'];
		$sql = "update `".TABLE_PREFIX."photo_album` set pic = 
					(select picUrl from `".TABLE_PREFIX."photo` where id = $id )
					where id = $aid  and uid={$_COOKIE['MEMBER_ID']}";
		$query = $this->DatabaseHandler->Query($sql);		
		$html = "<script type='text/javascript'>alert('设置封面成功！');location.href='index.php?mod=photo&code=album&id=$id';</script>";
		echo $html;
	}
	
	function album(){
		$id = $this->Get['id'];
		$sql = "select a.*, b.title, b.category, b.description, b.pic from `".TABLE_PREFIX."photo` as a
				left join `".TABLE_PREFIX."photo_album` as b on a.albumId=b.id where a.id=$id order by a.id desc";
		$query = $this->DatabaseHandler->Query($sql);
		$pInfo = $query->GetRow(); //图片信息
		
		//第一个图片
		$sql_first = "select id from `".TABLE_PREFIX."photo` where albumId={$pInfo['albumId']} order by addTime desc limit 1";
		$query_first = $this->DatabaseHandler->Query($sql_first);
		$pInfoFirst = $query_first->GetRow(); //图片信息
		$pInfo['album_first'] = $pInfoFirst['id'];

		//上一个图片
		$sql_prev = "select * from `".TABLE_PREFIX."photo` where id > $id 
				and userId={$pInfo['userId']} and albumId={$pInfo['albumId']} order by id asc limit 1";
		$query_prev = $this->DatabaseHandler->Query($sql_prev);
		$row_prev = $query_prev->GetRow();
		$pInfo['prev_url'] = empty($row_prev['id']) ? "#" : "index.php?mod=photo&code=album&id={$row_prev['id']}";
		$pInfo['prev_pic'] = empty($row_prev['picUrl']) ? "templates/app/images/photo/album_pre.gif" : "upload/photo/{$row_prev['picUrl']}";
		
		//下一张
		$sql_next = "select * from `".TABLE_PREFIX."photo` where id < $id 
				and userId={$pInfo['userId']} and albumId = {$pInfo['albumId']} order by id desc limit 1";
		$query_next = $this->DatabaseHandler->Query($sql_next);
		$row_next = $query_next->GetRow();
		$pInfo['next_url'] = empty($row_next['id']) ? "#" : "index.php?mod=photo&code=album&id={$row_next['id']}";
		$pInfo['next_pic'] = empty($row_next['picUrl']) ? "templates/app/images/photo/album_next.gif" : "upload/photo/{$row_next['picUrl']}";
	
		//评论
		$sqlc = "select a.*, b.username, b.nickname,b.face from `".TABLE_PREFIX."photo_comment` as a
				left join `".TABLE_PREFIX."members` as b on b.uid = a.userId  where a.photoId = $id order by a.addTime desc";
		$queryc = $this->DatabaseHandler->Query($sqlc);
		while ($rowc = $queryc->GetRow()){
			$comm_list[] = $rowc;
		}
		include(ROOT_PATH . './templates/app/photo_album.php');
	}
	
	
	function comm_add(){	
		$pid = $this->Get['pid'];
		$sql = "insert into  `".TABLE_PREFIX."photo_comment` (`userId`, `photoId`, `content`, `addTime`) values ( {$_COOKIE['MEMBER_ID']}, $pid, '{$this->Post['content']}','". date('Y-m-d H:i:s') ."' )";
		$query = $this->DatabaseHandler->Query($sql);		
		$html = "<script type='text/javascript'>alert('留言成功！');location.href='index.php?mod=photo&code=album&id=$pid';</script>";
		echo $html;
	}
	
	function comm_del(){	
		$id = $this->Get['id'];
		$pid = $this->Get['pid'];
		$sql = "delete from `".TABLE_PREFIX."photo_comment` where id=$id";
		$query = $this->DatabaseHandler->Query($sql);
		$html = "<script type='text/javascript'>alert('删除评论成功！');location.href='index.php?mod=photo&code=album&id=$pid';</script>";
		echo $html;
	}	
	
	
	function pic_del(){	
		$id = $this->Get['id'];
		$sql = "delete from `".TABLE_PREFIX."photo` where id=$id and userId={$_COOKIE['MEMBER_ID']}";
		$query = $this->DatabaseHandler->Query($sql);
		$html = "<script type='text/javascript'>alert('删除照片成功！');location.href='index.php?mod=photo&code=my';</script>";
		echo $html;
	}	

	
	
	function album_edit(){	
		$id = $this->Get['id'];
		$ref = $this->Get['ref'];
		if( !empty( $this->Post['album_title'] ) ){
				
			if( !empty($id) ){
				$sql = "update `".TABLE_PREFIX."photo_album` set `title` = '{$this->Post['album_title']}',
										 `category` = '{$this->Post['album_category']}', 
										 `description` = '{$this->Post['album_desc']}', 
										 `uid` = {$_COOKIE['MEMBER_ID']},  
										 `addtime` =  '". date('Y-m-d H:i:s') ."' 
								where id = $id";
				
				$query = $this->DatabaseHandler->Query($sql);
				$html = "相册修改成功！";
			}else{
				$sql = "insert into `".TABLE_PREFIX."photo_album` (`title`, `category`, `description`, `uid`, `addtime`) 
					values ( '{$this->Post['album_title']}', 
							'{$this->Post['album_category']}', 
							'{$this->Post['album_desc']}', 
							{$_COOKIE['MEMBER_ID']}, 
							'". date('Y-m-d H:i:s') ."' )";
				$query = $this->DatabaseHandler->Query($sql);
				$html = "相册添加成功！";
			}
		
		}else{
			$html = "相册名称不能为空！";
		}
		$html = "<script type='text/javascript'>alert('$html');location.href='index.php?mod=photo&code=$ref';</script>";
		echo $html;
	}
	
	function album_del(){	
		$id = $this->Get['id'];
		$sql = "delete from `".TABLE_PREFIX."photo_album` where id=$id and uid={$_COOKIE['MEMBER_ID']}";
		$sqla = "delete from `".TABLE_PREFIX."photo` where albumId=$id and userId={$_COOKIE['MEMBER_ID']}";
		$html = "删除相册成功！";
		if( !$this->DatabaseHandler->Query($sql) ){
			$html = "删除相册失败！";
		}
		if( !$this->DatabaseHandler->Query($sqla) ){
			$html = "删除失败！";
		}
		$html = "<script type='text/javascript'>alert('$html');location.href='index.php?mod=photo&code=my';</script>";
		echo $html;
	}
	
}

?>
